//
// Created by 23022 on 2022/5/11.
//
#include"filter.h"
#include "meanfilter.h"
#include<QDebug>
MeanFilter::MeanFilter(int size) : Filter(size) {

}

MeanFilter::~MeanFilter() {

}

Image MeanFilter::Filtering(const Image &input)
{

    int height = input.Height();
    int width = input.Width();
    Image output(height, width);
    int i, j, range[4] = {0}, pend, x, y;//range判断对应四个边缘
    double sum;
    pend = (filterSize - 1) / 2;
    for(int t=0;t<3;t++)
    for (i = 0; i < height; i++)
        for (j = 0; j < width; j++)
        {
            range[0] = i < pend ? 0 : (i - pend);//取滤波器的上边界
            range[1] = j < pend ? 0 : (j - pend);//取滤波器的左边界
            range[2] = (height - 1 < pend + i) ? height - 1 : (pend + i);//取滤波器的下边界
            range[3] = (width - 1 < pend + j) ? width - 1 : (pend + j);//取滤波器的右边界

            for (x = range[0]; x <= range[2]; ++x)
                for (y = range[1]; y <= range[3]; ++y)
                {
                    sum += input.At(x, y*3+t);
                }
            output.At(i, j*3+t) = sum / ((range[2] - range[0]+1)*(range[3] - range[1]+1));
            sum = 0;
      }
    return output;
}


